#include <stdio.h>
#include <stdlib.h>

typedef struct BTNode{
	int data;
	struct BTNode* LChild;
	struct BTNode* RChild;
} BTNode;

/**
 * 				0
 * 		1		|		2
 * 	3		5	|	6		4
 * 
 * 前序遍历:0135264 
 * 中序遍历:3150624 
 * 后续遍历:3516420 
 **/

//二叉树链式构建
void build_binary_tree(BTNode* head);

void display(BTNode* node);

//二叉树前序遍历
void pre_display(BTNode* head); 

//二叉树中序遍历
void in_display(BTNode* head); 

//二叉树后序遍历
void post_display(BTNode* head); 

 
int main()
{
	BTNode* head=(BTNode* )malloc(sizeof(BTNode));
	head->data=0;
	head->LChild=NULL;
	head->RChild=NULL; 
	build_binary_tree(head);
	//pre_display(head);
	//in_display(head);
	post_display(head);
	system("pause");
	return 0;
}

void build_binary_tree(BTNode* head)
{
	if(head==NULL)
	{
		head=(BTNode* )malloc(sizeof(BTNode));
		head->data=0;
		head->LChild=NULL;
		head->RChild=NULL;
	}
	
	BTNode* L_new_node=(BTNode* )malloc(sizeof(BTNode));
	L_new_node->data=1;
	L_new_node->LChild=NULL;
	L_new_node->RChild=NULL;
	head->LChild=L_new_node;

	BTNode* R_new_node=(BTNode* )malloc(sizeof(BTNode));
	R_new_node->data=2;
	R_new_node->LChild=NULL;
	R_new_node->RChild=NULL;
	head->RChild=R_new_node;	
	
	BTNode* L_new_node2=(BTNode* )malloc(sizeof(BTNode));
	L_new_node2->data=3;
	L_new_node2->LChild=NULL;
	L_new_node2->RChild=NULL;
	head->LChild->LChild=L_new_node2;
	
	BTNode * R_new_node2=(BTNode* )malloc(sizeof(BTNode));
	R_new_node2->data=4;
	R_new_node2->LChild=NULL;
	R_new_node2->RChild=NULL;
	head->RChild->RChild=R_new_node2;
	
	BTNode * R_new_node3=(BTNode* )malloc(sizeof(BTNode));
	R_new_node3->data=5;
	R_new_node3->LChild=NULL;
	R_new_node3->RChild=NULL;
	head->LChild->RChild=R_new_node3;


	BTNode * R_new_node4=(BTNode* )malloc(sizeof(BTNode));
	R_new_node4->data=6;
	R_new_node4->LChild=NULL;
	R_new_node4->RChild=NULL;
	head->RChild->LChild=R_new_node4;	
	
}

void display(BTNode* node)
{
	printf("%d\t%x\n",node->data,&node);
}

void pre_display(BTNode* head)
{
	if(head)
	{
		display(head);
		pre_display(head->LChild);
		pre_display(head->RChild);
	} 
}

void in_display(BTNode* head)
{
	if(head)
	{
		in_display(head->LChild);
		display(head);
		in_display(head->RChild);
	}
}

void post_display(BTNode* head)
{
	if(head)
	{
		post_display(head->LChild);
		post_display(head->RChild);
		display(head);
	}
}
